package echat.future.cn.echat.my.weight.Navagation;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import echat.future.cn.echat.R;

public class NavagationViews extends LinearLayout {
    private String title;
    TextView right;
    public NavagationViews(Context context) {
        this(context,null);
    }

    public NavagationViews(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NavagationViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CNavagation, 0, 0);
        title = a.getString(R.styleable.CNavagation_title);

        if(title == null){
            title = "标题";
        }
        String rightTitle = a.getString(R.styleable.CNavagation_rightTitle);
        a.recycle();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.navagation_bar,null);
        view.findViewById(R.id.back).setOnClickListener(v -> ((Activity)(getContext())).finish());
       TextView textView =  view.findViewById(R.id.title);
       textView.setText(title);

        right =  view.findViewById(R.id.right_title);
        if(rightTitle == null){
            rightTitle = "";
        }
        right.setText(rightTitle);
       view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
       addView(view);
    }

    public void setOnReightClick(OnClickListener onRightClick){
        if(right!=null){
            right.setOnClickListener(onRightClick);
        }
    }
}
